---
title: "Reference: Workflow Class | Workflows"
description: Documentation for the `Workflow` class in Mastra, which enables you to create state machines for complex sequences of operations with conditional branching and data validation.
---

# Workflow Class

The `Workflow` class enables you to create state machines for complex sequences of operations with conditional branching and data validation.

## Usage example

```typescript title="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
import { createWorkflow } from "@mastra/core/workflows";
import { z } from "zod";

export const workflow = createWorkflow({
  id: "test-workflow",
  inputSchema: z.object({
    value: z.string(),
  }),
  outputSchema: z.object({
    value: z.string(),
  }),
});
```

## Constructor parameters

<PropertiesTable
  content={[
    {
      name: "id",
      type: "string",
      description: "Unique identifier for the workflow",
    },
    {
      name: "inputSchema",
      type: "z.ZodType<any>",
      description: "Zod schema defining the input structure for the workflow",
    },
    {
      name: "outputSchema",
      type: "z.ZodType<any>",
      description: "Zod schema defining the output structure for the workflow",
    },
    {
      name: "stateSchema",
      type: "z.ZodObject<any>",
      description:
        "Optional Zod schema for the workflow state. Automatically injected when using Mastra's state system. If not specified, type is 'any'.",
      isOptional: true,
    },
    {
      name: "options",
      type: "WorkflowOptions",
      description: "Optional options for the workflow",
      isOptional: true,
    },
  ]}
/>

### WorkflowOptions

<PropertiesTable
  content={[
    {
      name: "tracingPolicy",
      type: "TracingPolicy",
      description: "Optional tracing policy for the workflow",
      isOptional: true,
    },
    {
      name: "validateInputs",
      type: "boolean",
      description:
        "Optional flag to determine whether to validate the workflow inputs. This also applies default values from zodSchemas on the workflow/step input/resume data. If input/resume data validation fails on start/resume, the workflow will not start/resume, it throws an error instead. If input data validation fails on a step execution, the step fails, causing the workflow to fail and the error is returned.",
      isOptional: true,
      defaultValue: "true",
    },
    {
      name: "shouldPersistSnapshot",
      type: "(params: { stepResults: Record<string, StepResult<any, any, any, any>>; workflowStatus: WorkflowRunStatus }) => boolean",
      description:
        "Optional flag to determine whether to persist the workflow snapshot",
      isOptional: true,
      defaultValue: "() => true",
    },
  ]}
/>

## Running with initial state

When starting a workflow run, you can pass `initialState` to set the starting values for the workflow's state:

```typescript showLineNumbers copy
const run = await workflow.createRun();

const result = await run.start({
  inputData: { value: "hello" },
  initialState: {
    counter: 0,
    items: [],
  },
});
```

The `initialState` object should match the structure defined in the workflow's `stateSchema`. See [Workflow State](/docs/v1/workflows/workflow-state) for more details.

## Workflow status

A workflow's `status` indicates its current execution state. The possible values are:

<PropertiesTable
  content={[
    {
      name: "success",
      type: "string",
      description:
        "All steps finished executing successfully, with a valid result output",
    },
    {
      name: "failed",
      type: "string",
      description:
        "Workflow encountered an error during execution, with error details available",
    },
    {
      name: "suspended",
      type: "string",
      description:
        "Workflow execution is paused waiting for resume, with suspended step information",
    },
  ]}
/>

## Related

- [Step Class](./step)
- [Workflow State](/docs/v1/workflows/workflow-state)
- [Control flow](/docs/v1/workflows/control-flow)
